﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using System;
using System.Collections.Generic;
using System.Text;
namespace UColor.Core.Helpers {
	public class EscapeHelper {
		List<Dictionary<string, string>> replaces = new List<Dictionary<string, string>>();
		List<Dictionary<string, string>> inverts = new List<Dictionary<string, string>>();
		public void AddPair(string original, string escaped) {
			AddReplace(original, escaped, replaces);
			AddReplace(escaped, original, inverts);
		}
		public string Screen(string s) {
			return Replace(s, replaces);
		}
		public string Unscreen(string s) {
			return Replace(s, inverts);
		}
		static void AddReplace(string from, string to, List<Dictionary<string, string>> replaces) {
			Dictionary<string, string> replace = GetDictionary(replaces, from.Length - 1);
			replace.Add(from, to);
		}
		static string Replace(string s, List<Dictionary<string, string>> replaces) {
			StringBuilder result = new StringBuilder();
			for(int i = 0; i < s.Length; ++i) {
				int originalLength_1 = -1;
				string escaped = null;
				for(int length_1 = replaces.Count; --length_1 >= 0; ) {
					if(i + length_1 + 1 > s.Length) continue;
					Dictionary<string, string> dictionary = GetDictionary(replaces, length_1);
					string t = s.Substring(i, length_1 + 1);
					string r;
					if(dictionary.TryGetValue(t, out r)) {
						originalLength_1 = length_1;
						escaped = r;
						break;
					}
				}
				if(originalLength_1 >= 0) {
					result.Append(escaped);
					i += originalLength_1;
				} else {
					result.Append(s[i]);
				}
			}
			return result.ToString();
		}
		static Dictionary<string, string> GetDictionary(List<Dictionary<string, string>> list, int i) {
			if(i < list.Count) return list[i];
			for(int k = i - list.Count + 1; --k >= 0; ) {
				list.Add(new Dictionary<string, string>());
			}
			return list[i];
		}
	}
}
